package com.ruoyi.custom.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.constant.CustomConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.custom.domain.TCustomInfo;
import com.ruoyi.custom.service.ITCustomInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户信息Controller
 * 
 * @author wzy
 * @date 2025-01-03 09:15:59
 */
@RestController
@RequestMapping("/customs/custom")
public class TCustomInfoController extends BaseController
{
    @Autowired
    private ITCustomInfoService tCustomInfoService;

    /**
     * 查询客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('customs:custom:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCustomInfo tCustomInfo)
    {
        startPage();
        List<TCustomInfo> list = tCustomInfoService.selectTCustomInfoList(tCustomInfo);
        return getDataTable(list);
    }

    /**
     * 导出客户信息列表
     */
    @PreAuthorize("@ss.hasPermi('customs:custom:export')")
    @Log(title = "客户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TCustomInfo tCustomInfo)
    {
        List<TCustomInfo> list = tCustomInfoService.selectTCustomInfoList(tCustomInfo);
        ExcelUtil<TCustomInfo> util = new ExcelUtil<TCustomInfo>(TCustomInfo.class);
        util.exportExcel(response, list, "客户信息数据");
    }

    /**
     * 获取客户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('customs:custom:query')")
    @GetMapping(value = "/{customerId}")
    public AjaxResult getInfo(@PathVariable("customerId") Long customerId)
    {
        return success(tCustomInfoService.selectTCustomInfoByCustomerId(customerId));
    }

    /**
     * 新增客户信息
     */
    @PreAuthorize("@ss.hasPermi('customs:custom:add')")
    @Log(title = "客户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TCustomInfo tCustomInfo)
    {
        if (CustomConstants.NOT_UNIQUE.equals(tCustomInfoService.checkCustomerNameUnique(tCustomInfo)))
        {
            return error("新增客户'" + tCustomInfo.getCustomerName() + "'失败，客户名称已存在");
        }
        tCustomInfo.setCreateBy(getNickName());
        return toAjax(tCustomInfoService.insertTCustomInfo(tCustomInfo));
    }

    /**
     * 修改客户信息
     */
    @PreAuthorize("@ss.hasPermi('customs:custom:edit')")
    @Log(title = "客户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TCustomInfo tCustomInfo)
    {
        if (CustomConstants.NOT_UNIQUE.equals(tCustomInfoService.checkCustomerNameUnique(tCustomInfo)))
        {
            return error("修改客户'" + tCustomInfo.getCustomerName() + "'失败，客户名称已存在");
        }
        tCustomInfo.setUpdateBy(getNickName());
        return toAjax(tCustomInfoService.updateTCustomInfo(tCustomInfo));
    }

    /**
     * 删除客户信息
     */
    @PreAuthorize("@ss.hasPermi('customs:custom:remove')")
    @Log(title = "客户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{customerIds}")
    public AjaxResult remove(@PathVariable Long[] customerIds)
    {
        return toAjax(tCustomInfoService.deleteTCustomInfoByCustomerIds(customerIds));
    }

    /**
     * 查询客户信息字典项
     */
//    @PreAuthorize("@ss.hasPermi('customs:custom:list')")
    @GetMapping("/listoption")
    public AjaxResult listoption()
    {
//        List<TCustomInfo> list = tCustomInfoService.selectTCustomInfoAll();
//        return getDataTable(list);
//        AjaxResult ajax = AjaxResult.success();
//        ajax.put("customs", tCustomInfoService.selectTCustomInfoAll());
//        return ajax;
        return success(tCustomInfoService.selectTCustomInfoAll());
    }
}
